//
//  DMOrderDetail.m
//  DMGME
//
//  Created by ihope99 on 15/8/31.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMOrderDetail.h"
#import "DMOrderDetailCell.h"
#import "DMoeferDetailModel.h"
#import "UIImageView+WebCache.h"

@interface DMOrderDetail ()
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *datas;
@property (nonatomic,strong) NSDictionary *dic;
@end

@implementation DMOrderDetail
{
    UIView *headerView;
    UIView *footTotalView;
}

- (void)viewDidLoad {

    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 35)];
    titleLabel.text = @"订单详情";
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;

    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBackToBack)];
    
    _datas = [NSMutableArray array];
    _dic = [NSDictionary dictionary];

    //创建tableView
    [self createTableView];
    if ([_ident  isEqual: @"YES"]) {
        [self downloadData:_urls];
    }else
    {
        [self downloadData];

    }

}
-(void)GoBackToBack
{
    if ([_ident  isEqual: @"YES"]) {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1]  animated:YES];
    }else
    {
        [self GoBack];
    }
}
-(void)downloadData:(NSString *)mid
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$index"] forKey:@"token"];
    [dict setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setValue:@"index" forKey:@"action"];
    [dict setObject:mid forKey:@"order_id"];
    [AFDownManager POST:MyWalletDetailNet parameters:dict success:^(id responseObject) {
//        NSLog(@"订单详情 == %@",responseObject);
        [_datas removeAllObjects];
        NSArray *arr = responseObject[@"data"][@"goods"];
        
        for (NSDictionary *dic in arr) {
//            NSLog(@"ww==%@",responseObject);
            DMoeferDetailModel *dModel = [[DMoeferDetailModel alloc] init];
            [dModel setValuesForKeysWithDictionary:dic];
            [_datas addObject:dModel];
            
        }
        _dic = responseObject[@"data"];
        
        //    NSLog(@"ss==%@",_ShopDic);
        [_tableView reloadData];
    } failure:^(NSError *error) {
        [self showMsg:@"网络连接故障"];
    }];
    
}

-(void)downloadData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$index"] forKey:@"token"];
    [dict setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setValue:@"index" forKey:@"action"];
    [dict setObject:_ordewrID forKey:@"order_id"];
    [AFDownManager POST:MyWalletDetailNet parameters:dict success:^(id responseObject) {
//        NSLog(@"订单详情 == %@",responseObject);
        [_datas removeAllObjects];
        NSArray *arr = responseObject[@"data"][@"goods"];
        
        for (NSDictionary *dic in arr) {
//         NSLog(@"ww==%@",responseObject);
            DMoeferDetailModel *dModel = [[DMoeferDetailModel alloc] init];
            [dModel setValuesForKeysWithDictionary:dic];
            [_datas addObject:dModel];
        
        }
        _dic = responseObject[@"data"];
//    NSLog(@"ss==%@",_ShopDic);
        [_tableView reloadData];
    } failure:^(NSError *error) {
        [self showMsg:@"网络连接故障"];
    }];

}

-(NSString *)timeStamaps:(NSString *)str
{
    //时间戳转化为时间字符串
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:[str longLongValue]];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:MM:ss"];
    NSString *string = [formatter stringFromDate:date];
//    NSLog(@"date1:%@",str);
    return string;
}
- (void)createTableView{
   
     _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    _tableView.delegate = self;
    _tableView.dataSource = self;


    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[DMOrderDetailCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datas.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"cell";
    DMOrderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
     DMoeferDetailModel *dModel = _datas[indexPath.row];
    if (dModel.cover == nil) {
        cell.headImageView.image = [UIImage imageNamed:@""];
    }
    if (dModel.period == nil) {
        cell.typeLable.text = @"0期";
    }
    if (dModel.title) {
        
        cell.headLable.text = @"无标题";
    }
    if (dModel.num == 0) {
        cell.numLable.text = @"x0";
    }
    if (dModel.price == nil) {
        cell.moneyLable.text = @"￥";
    }
    cell.dModel = dModel;

    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 225;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
//    DMOrderDetail *orderDetail = [[DMOrderDetail alloc] init];
//    [self.navigationController pushViewController:orderDetail animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, KWidth, 225);
//    headerView.backgroundColor = [UIColor magentaColor];
    //订单号
    UILabel *orderNum = [[UILabel alloc] init];
    orderNum.frame = CGRectMake(15, 20, KWidth-30, 12);
    orderNum.font = [UIFont systemFontOfSize:14];

    if (_dic[@"order_sn"] == NULL) {
        orderNum.text = [NSString stringWithFormat:@"订  单  号:"];
    }
   else
   {
    orderNum.text = [NSString stringWithFormat:@"订  单  号: %@",_dic[@"order_sn"]];
//    NSLog(@"_dic[order_sn] == %@",_dic[@"order_sn"]);
   }
    //下单时间
    UILabel *orderTime = [[UILabel alloc] init];
   
    orderTime.frame = CGRectMake(15, CGRectGetMaxY(orderNum.frame)+8, KWidth-30, 12);
    orderTime.font = [UIFont systemFontOfSize:14];
    if (_dic[@"order_time"] == NULL) {
        orderTime.text = [NSString stringWithFormat:@"下单时间: 0000-00-00 00:00:00"];
    }else
    {
    orderTime.text = [NSString stringWithFormat:@"下单时间: %@",[self timeStamaps:_dic[@"order_time"]]];
    }
    
    //确认订单时间
    UILabel *confirmOrderTime = [[UILabel alloc] init];
    confirmOrderTime.frame = CGRectMake(15, CGRectGetMaxY(orderTime.frame)+8, KWidth-30, 12);
    confirmOrderTime.text = [NSString stringWithFormat:@"确认订单时间: %@",[self timeStamaps:_dic[@"cofirm_time"]]];
    confirmOrderTime.font = [UIFont systemFontOfSize:14];
    
    //购买人
    UILabel *purchase = [[UILabel alloc] init];
    purchase.frame = CGRectMake(15, CGRectGetMaxY(confirmOrderTime.frame)+8, KWidth-30, 12);
    purchase.font = [UIFont systemFontOfSize:14];
    if (_dic[@"consignee"] == NULL) {
        purchase.text = [NSString stringWithFormat:@"购  买  人: "];
    }else
    {
    purchase.text = [NSString stringWithFormat:@"购  买  人:  %@",_dic[@"consignee"]];
    }

    //购买人手机号
    UILabel *purchasePhone = [[UILabel alloc] init];
    purchasePhone.frame = CGRectMake(15, CGRectGetMaxY(purchase.frame)+8, KWidth-30, 12);
    purchasePhone.font = [UIFont systemFontOfSize:14];
    if(_dic[@"mobile"] == NULL)
    {
      purchasePhone.text = [NSString stringWithFormat:@"购买人手机号:  "];
    }else
    {
    purchasePhone.text = [NSString stringWithFormat:@"购买人手机号:  %@",_dic[@"mobile"]];
    }
    
    //线
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0, CGRectGetMaxY(purchasePhone.frame)+15, KWidth, 1);
    lineView.backgroundColor = [UIColor grayColor];
    //店铺信息
    UILabel *storeInfo = [[UILabel alloc] init];
    storeInfo.frame = CGRectMake(15, CGRectGetMaxY(lineView.frame)+8, KWidth-30, 10);
    
    storeInfo.font = [UIFont systemFontOfSize:13];
    if (_dic[@"storename"] == NULL) {
        storeInfo.text = [NSString stringWithFormat:@"加载..."];

    }else
    {
        storeInfo.text = [NSString stringWithFormat:@" %@",_dic[@"storename"]];
    }
    UIImageView *headeImage = [[UIImageView alloc] init];
    headeImage.frame = CGRectMake(20, CGRectGetMaxY(storeInfo.frame)+10, 40, 40);
    headeImage.clipsToBounds = YES;
    headeImage.layer.cornerRadius = 20;
    if (_dic[@"cover"] == NULL) {
        headeImage.backgroundColor = [UIColor colorWithWhite:230/255.0 alpha:1];

    }
    else
    {
        [headeImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_dic[@"cover"]]]];
    }
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.frame = CGRectMake(CGRectGetMaxX(headeImage.frame)+20, Y(headeImage), KWidth - 100, HEIGHT(headeImage));
    
    nameLabel.font = [UIFont systemFontOfSize:14];
    if (_dic[@"realname"] == NULL) {
        nameLabel.text =[NSString stringWithFormat:@"加载..."];
    }
    else
    {
        nameLabel.text =[NSString stringWithFormat:@"%@",_dic[@"realname"]];
    }
    
    UILabel *adressLable = [[UILabel alloc] init];
    adressLable.frame = CGRectMake(X(headeImage), CGRectGetMaxY(nameLabel.frame)+10, KWidth-100, 10);
    adressLable.font = [UIFont systemFontOfSize:13];
    if (_dic[@"address"] == NULL) {
        adressLable.text = [NSString stringWithFormat:@"地址: "];
    }
    else
    {
        adressLable.text = [NSString stringWithFormat:@"地址: %@",_dic[@"address"]];
    }
    UILabel *namePhone = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(headeImage.frame)+120, Y(headeImage), 150, HEIGHT(headeImage))];
    namePhone.font = [UIFont systemFontOfSize:14];
    namePhone.text = [NSString stringWithFormat:@"手机号: %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"mobile"]];
 
    [headerView addSubview:namePhone];
    [headerView addSubview:orderNum];
    [headerView addSubview:orderTime];
    [headerView addSubview:confirmOrderTime];
    [headerView addSubview:purchase];
    [headerView addSubview:purchasePhone];
    [headerView addSubview:lineView];
    [headerView addSubview:storeInfo];
    [headerView addSubview:headeImage];
    [headerView addSubview:nameLabel];
    [headerView addSubview:adressLable];
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    footTotalView = [[UIView alloc] init];
    footTotalView.frame = CGRectMake(0, 0, KWidth, 50);
    footTotalView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0, 0, KWidth, 1);
    lineView.backgroundColor = [UIColor grayColor];
    [footTotalView addSubview:lineView];
    
    UILabel *timeLable = [[UILabel alloc] init];
    timeLable.frame = CGRectMake(15, 5, 130, 10);
   
    timeLable.text = [NSString stringWithFormat:@"%@",[self timeStamaps:_dic[@"finish_time"]]];;
    timeLable.font = [UIFont systemFontOfSize:12];
    timeLable.textAlignment = NSTextAlignmentLeft;
    [footTotalView addSubview:timeLable];
    if ([_ident  isEqual: @"YES"]) {
        timeLable.hidden = YES;
    }
    else
    {
        timeLable.hidden = NO;
    }
    UILabel *totalNum = [[UILabel alloc] init];
    totalNum.frame = CGRectMake(KWidth - 90, 5, 80, 15);
    totalNum.textAlignment = NSTextAlignmentLeft;
    if (_dic[@"all_total"] == NULL) {
        totalNum.text = [NSString stringWithFormat:@"合计:￥0.00"];
    }
    else
    {
    totalNum.text = [NSString stringWithFormat:@"合计:￥%@",_dic[@"all_total"] ];
    }
    totalNum.font = [UIFont systemFontOfSize:12];
    [footTotalView addSubview:totalNum];
    
    UILabel *subsidyNum = [[UILabel alloc] init];
    subsidyNum.frame = CGRectMake(KWidth - 90, CGRectGetMaxY(totalNum.frame)+5, 80, 20);
    subsidyNum.textAlignment = NSTextAlignmentLeft;
    if (_dic[@"subsidy"] == [NSNull null]) {
       subsidyNum.text = [NSString stringWithFormat:@"补贴:￥0.00"];
    }else
    {
    subsidyNum.text = [NSString stringWithFormat:@"补贴:￥%@",_dic[@"subsidy"]];
    }
    subsidyNum.font = [UIFont systemFontOfSize:14];
    [footTotalView addSubview:subsidyNum];
    return footTotalView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
